#include "debugform.h"
#include "ui_debugform.h"

DebugForm::DebugForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DebugForm)
{
    ui->setupUi(this);
    initMembers();
}

DebugForm::~DebugForm()
{
    delete ui;
}

void DebugForm::on_send_P_B_clicked()
{
    if(currentType == HD_CONNECTED){

    }
}

void DebugForm::on_disConnect_clicked()
{
    ui->lb_link->setText("false") ;
    HandleHelper::GetInstance()->StopService();
    RobotHelper::GetInstance()->CloseRobot();
    currentType = HD_NOT_CONNECT;
    ui->reCon_B->setText("连接");
}

void DebugForm::on_reCon_B_clicked()
{
    HandleHelper::GetInstance()->ConnectHandlerExec();
    currentType = HD_CONNECTED;
    updateUI->start(100);
    ui->reCon_B->setText("重新连接");
    ui->textBrowser->append("Handler Link Sucessed!");
}

/// Ui update
void DebugForm::on_UIsUpdate(){
    ui->lb_link->setText( (currentType == HD_CONNECTED) ? "true":"false");
    ui->lb_serialN->setText(SerialHelper::GetInstance()->p()->pInfo.name.c_str());
    ui->lb_bau->setText(QString::number( SerialHelper::GetInstance()->p()->pInfo.port));
}

void DebugForm::initMembers()
{
    updateUI = new QTimer;
    connect(updateUI,&QTimer::timeout,this,&DebugForm::on_UIsUpdate);
}

void DebugForm::initUIControls()
{
    if(T_USB == HandleHelper::GetInstance()->comType ){
        ui->bau_B->setEnabled(false);
        ui->lb_serialN->setEnabled(false);
        ui->lb_bau->setEnabled(false);
    }
}

void DebugForm::on_run_B_clicked()
{
    RobotPos temp = {0,0,0,0,0,0,FLANGE};
    int direct = (0 == ui->cB_direct->currentIndex() ) ? 1:-1;
    double _move = ui->cB_Speed_main->currentText().toDouble(new bool) + ui->cB_speed_child->currentText().toDouble(new bool);
    double _speed = ui->cB_percent->currentText().toDouble(new bool);

    if(500 < _move){
        _move = 100;
    }
    switch (curentAXis) {
    case 0:
        temp.x = _move*direct;
        break;
    case 1:
        temp.y = _move*direct;
        break;
    case 2:
        temp.z = _move*direct;
        break;
    case 3:
        temp.a = _move*direct;
        break;
    case 4:
        temp.b = _move*direct;
        break;
    case 5:
        temp.c = _move*direct;
        break;
    default:
        break;
    }

    RobotHelper::GetInstance()->RelativeMove(temp,(100<_speed)?100:_speed);
}

void DebugForm::on_cB_Select_activated(int index)
{
    curentAXis = index;

}

void DebugForm::on_go_B_clicked()
{
    RobotPos temp = {0,0,0,0,0,0,FLANGE};
    bool ret = str2Robot( ui->line_Pos->text().toStdString(),temp);
    if(ret)
        RobotHelper::GetInstance()->ABSMove(temp);
    else
        ui->textBrowser->append("Error Str to RobotPos!");
}

void DebugForm::on_comMax_activated(const QString &arg1)
{
    RobotHelper::GetInstance()->T_P_ONCE_MAX = arg1.toInt(new bool);
}

void DebugForm::on_zero_B_clicked()
{
    HandleHelper::GetInstance()->SetMidAxis();
}
